/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package general.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 *
 * @author alexander
 */
public class MyDateUtil {

    private static SimpleDateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat reducedSimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    private static SimpleDateFormat moreReducedSimpleDateFormat = new SimpleDateFormat("dd/MM/yy");
    private static SimpleDateFormat extenseSimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");
    private static SimpleDateFormat reducedTimeFormat = new SimpleDateFormat("HH:mm:ss");

    public static String getMonthText(int value){
        switch(value){
            case Calendar.JANUARY:{
                return "Janeiro";
            }
            case Calendar.FEBRUARY:{
                return "Fevereiro";
            }
            case Calendar.MARCH:{
                return "Março";
            }
            case Calendar.APRIL:{
                return "Abril";
            }
            case Calendar.MAY:{
                return "Maio";
            }
            case Calendar.JUNE:{
                return "Junho";
            }
            case Calendar.JULY:{
                return "Julho";
            }
            case Calendar.AUGUST:{
                return "Agosto";
            }
            case Calendar.SEPTEMBER:{
                return "Setembro";
            }
            case Calendar.OCTOBER:{
                return "Outubro";
            }
            case Calendar.NOVEMBER:{
                return "Novembro";
            }
            case Calendar.DECEMBER:{
                return "Dezembro";
            }
        }

        return "Nenhum";
    }

    public static String getWeekText(int value) {
        switch (value) {
            case 0:
                return "Domingo";
            case 1:
                return "Segunda-Feira";
            case 2:
                return "Terça-Feira";
            case 3:
                return "Quarta-Feira";
            case 4:
                return "Quinta-Feira";
            case 5:
                return "Sexta-Feira";
            case 6:
                return "Sabado";
        }

        return "Inválido";
    }

    public static String getReducedWeekText(int value) {
        switch (value) {
            case 0:
                return "Dom";
            case 1:
                return "Seg";
            case 2:
                return "Ter";
            case 3:
                return "Qua";
            case 4:
                return "Qui";
            case 5:
                return "Sex";
            case 6:
                return "Sab";
        }

        return "Inválido";
    }

    public static String getFormattedTime(int time) {
        int hour = time / 60;
        int minute = time % 60;

        String S_hour = hour < 10 ? "0" + hour : "" + hour;
        String S_minute = minute < 10 ? "0" + minute : "" + minute;

        return S_hour + ":" + S_minute;
    }

    public static int getWeekDay(Date date) {
        if (date == null) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public static String getWeekAndDateString(Date date) {
        if (date == null) {
            return "-------";
        }

        return getWeekText(getWeekDay(date)) + " " + getReducedDateString(date);
    }

    public static String getReducedDateString(Date date) {
        if (date == null) {
            return "------";
        }

        return reducedSimpleDateFormat.format(date);
    }

    public static String getReducedTimeString(Date date) {
        if (date == null) {
            return "------";
        }

        return reducedTimeFormat.format(date);
    }

    public static String getMoreReducedDateString(Date date) {
        if (date == null) {
            return "------";
        }

        return moreReducedSimpleDateFormat.format(date);
    }

    public static String getDateStringForPeriod(Date date) {
        if (date == null) {
            return "------";
        }

        return String.format("%s(%s)", reducedSimpleDateFormat.format(date), getWeekText(getWeekDay(date)));
    }

    public static String getScheduleTipsColumnTitle(Date date) {
        return getReducedWeekText(getWeekDay(date)) + "-" + getReducedDateString(date);
    }

    public static String getExtenseDateString(Date date) {
        if (date == null) {
            return "------";
        }

        return extenseSimpleDateFormat.format(date);
    }

    public static Date getDateWithTime(Date date, int hour, int minute, int second) {
        //method 2
        Calendar cal = getCalendar(date);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, second);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static Calendar getCalendar(Date date){
        Calendar cal = Calendar.getInstance(new Locale("pt", "BR"));
        cal.setTimeZone(TimeZone.getTimeZone("GMT-3"));
        cal.setTime(date);

        return cal;
    }

    public static Integer getIdade(Date data) {

        Calendar dataNascimento = getCalendar(data);
        Calendar dataAtual = getCalendar(new Date());

        Integer diferencaMes = dataAtual.get(Calendar.MONTH) - dataNascimento.get(Calendar.MONTH);
        Integer diferencaDia = dataAtual.get(Calendar.DAY_OF_MONTH) - dataNascimento.get(Calendar.DAY_OF_MONTH);

        Integer idade = (dataAtual.get(Calendar.YEAR) - dataNascimento.get(Calendar.YEAR));

        if (diferencaMes < 0 || (diferencaMes == 0 && diferencaDia < 0)) {
            idade--;
        }

        return idade;
    }



    public static boolean itsToday(Date date) {
        Calendar cal = getCalendar(date);
        int day = cal.get(Calendar.DAY_OF_MONTH);//date.getDay();
        int month = cal.get(Calendar.MONTH);//date.getMonth();
        Calendar currentTimeCal = getCalendar(new Date());
        int dayCurrent = currentTimeCal.get(Calendar.DAY_OF_MONTH);//currentDate.getDay();
        int monthCurrent = currentTimeCal.get(Calendar.MONTH);//currentDate.getMonth();

        if (monthCurrent == month && dayCurrent == day) {
            return true;
        } else {
            return false;
        }
    }

    public static Integer getCurrentHour() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        return c.get(Calendar.HOUR_OF_DAY);
    }

    public static Integer getCurrentMinute() {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        return c.get(Calendar.MINUTE);
    }

    public static Date incrementDays(Date date, Integer incrementDays) {
        if (incrementDays == null || incrementDays == 0) {
            return new Date(date.getTime());
        }
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTimeInMillis(date.getTime());
        c.add(Calendar.DAY_OF_YEAR, incrementDays);

        return c.getTime();
    }

    public static Date incrementMonths(Date date, Integer incrementMonths) {
        if (incrementMonths == null || incrementMonths == 0) {
            return new Date(date.getTime());
        }
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTimeInMillis(date.getTime());
        c.add(Calendar.MONTH, incrementMonths);

        return c.getTime();
    }

    public static Date incrementYears(Date date, Integer incrementYears) {
        if (incrementYears == null || incrementYears == 0) {
            return new Date(date.getTime());
        }
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTimeInMillis(date.getTime());
        c.add(Calendar.YEAR, incrementYears);

        return c.getTime();
    }

    public static Integer getHourAndMinuteInMinute(Date date) {
        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTime(date);

        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return hour * 60 + minute;
    }

    public static Integer getHour(Date date) {
        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTime(date);

        int hour = c.get(Calendar.HOUR_OF_DAY);

        return hour;
    }

    public static Integer getMonth(Date date) {
        if (date == null) {
            return null;
        }

        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTime(date);

        int hour = c.get(Calendar.MONTH);

        return hour;
    }

    public static int diffDays(Date now, Date other) {
        if (now == null || other == null) {
            return 0;
        }

        long first = now.getTime();
        long second = other.getTime();

        if (first > second) {
            long temp = second;
            second = first;
            first = temp;
        }

        return (int) ((second - first) / (1000l * 60l * 60l * 24l));
    }

    public static Date getDateWithHourAndMinute(int minute) {
        Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        c.setTime(new Date(0l));

        int hour = minute / 60;
        minute = minute % 60;
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);

        return c.getTime();
    }


    public static String getSQLDateFormat(Date date) {
        return sqlDateFormat.format(date);
    }

    public static Date getCurrentDay() {
        return getDateWithTime(new Date(System.currentTimeMillis()), 0, 0, 0);
    }

    public static boolean isValidDay(Date baseTime, boolean mondayFrequency, boolean tuesdayFrequency, boolean wednesdayFrequency, boolean thursdayFrequency, boolean fridayFrequency, boolean saturdayFrequency, boolean sundayFrequency) {
        int weekday = getWeekDay(baseTime);

        if (weekday == 0 && sundayFrequency) {
            return true;
        } else if (weekday == 1 && mondayFrequency) {
            return true;
        } else if (weekday == 2 && tuesdayFrequency) {
            return true;
        } else if (weekday == 3 && wednesdayFrequency) {
            return true;
        } else if (weekday == 4 && thursdayFrequency) {
            return true;
        } else if (weekday == 5 && fridayFrequency) {
            return true;
        } else if (weekday == 6 && saturdayFrequency) {
            return true;
        }

        return false;
    }

    public static boolean haveSameDayMonthYear(Date date1, Date date2){

        Calendar cal1 = getCalendar(date1);
        Calendar cal2 = getCalendar(date2);

        int day1 = cal1.get(Calendar.DAY_OF_MONTH);
        int month1 = cal1.get(Calendar.MONTH);
        int year1 = cal1.get(Calendar.YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_MONTH);
        int month2 = cal2.get(Calendar.MONTH);
        int year2 = cal2.get(Calendar.YEAR);

        return (day1 == day2 && month1 == month2 && year1 == year2);
    }

    public static boolean haveSameDayAndMonth(Date date1, Date date2){

        Calendar cal1 = getCalendar(date1);
        Calendar cal2 = getCalendar(date2);

        int day1 = cal1.get(Calendar.DAY_OF_MONTH);
        int month1 = cal1.get(Calendar.MONTH);
        int day2 = cal2.get(Calendar.DAY_OF_MONTH);
        int month2 = cal2.get(Calendar.MONTH);

        return (day1 == day2 && month1 == month2);
    }

    public static boolean haveSameYear(Date date1, Date date2){
        Calendar cal1 = getCalendar(date1);
        Calendar cal2 = getCalendar(date2);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        return (year1 == year2);
    }

    public static Date nextValidDay(Date baseTime, boolean mondayFrequency, boolean tuesdayFrequency, boolean wednesdayFrequency, boolean thursdayFrequency, boolean fridayFrequency, boolean saturdayFrequency, boolean sundayFrequency) {

        if (baseTime == null) {
            return null;
        }

        if (!mondayFrequency && !tuesdayFrequency && !wednesdayFrequency && !thursdayFrequency && !fridayFrequency && !saturdayFrequency && !sundayFrequency) {
            return null;
        }

        baseTime = incrementDays(baseTime, 1);
        for (int i = 0; i < 10; i++) {
            if (isValidDay(baseTime, mondayFrequency, tuesdayFrequency, wednesdayFrequency, thursdayFrequency, fridayFrequency, saturdayFrequency, sundayFrequency)) {
                return baseTime;
            }
            baseTime = incrementDays(baseTime, 1);
        }

        return null;

    }

  /*
   * Este metodo retorna uma string com os minutos, hora, dia, mês e ano.
   * O seu retorno é usado para dar um numero unico ao pedido
   *
   */
  public static  String getMinuteHourDaysMonthYearForPedido(){

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
        calendar.setTime(new Date());

        int minute = calendar.get(Calendar.MINUTE);
        int hour = calendar.get(Calendar.HOUR);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int month = calendar.get(Calendar.MONTH);
        int year = calendar.get(Calendar.YEAR);
        month += 1;
           System.out.println("Numeração pedido: "+hour+""+minute+""+day+""+month+"/"+year);
           System.out.println("Numeração pedido: Hora: "+hour);
           System.out.println("Numeração pedido: minute "+minute);
           System.out.println("Numeração pedido: day "+day);
           System.out.println("Numeração pedido: month "+month);
           System.out.println("Numeração pedido: ano "+year);
        return ""+hour+""+minute+""+day+""+month+"/"+year;
    }

}
